package com.wxc.wxcwzq.web.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.wxc.wxcwzq.core.util.ResultDao;
import com.wxc.wxcwzq.core.util.ReturnUtil;
import com.wxc.wxcwzq.web.entity.SysRoles;
import com.wxc.wxcwzq.web.service.SysRoleService;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Controller
@RequestMapping("/wxc")
public class SysRoleController {

	@Autowired
	private SysRoleService roleService;
	/**
	 * 角色管理主页面
	 */
	@RequestMapping("/role/roleList")
	@RequiresPermissions("roleInfo:list")
	public String rolePage(){
		log.info("访问角色列表：");
		return "page/systemSetting/roleList";
	}
	
	/**
	 * 加载角色列表数据
	 */
	@RequestMapping("/role/queryRoleList")
	@ResponseBody
	public String queryRoleList(SysRoles role,Integer page, Integer limit){
		log.info("查询角色信息，请求参数：{},{},{}","page:"+page,"limit:"+limit,"role:"+role.getRole());
		Page <SysRoles> list = roleService.queryAllRoleInfo(role, page, limit);
		ResultDao result = new ResultDao();
		result.setCode(0);
		result.setMsg("失败请重试");
		result.setCount(list.getTotal());
		result.setData(list.getResult());
		return JSON.toJSONString(result);
	}
	
	@RequestMapping("/role/addPage")
	@RequiresPermissions("roleInfo:add")
	public String addPage(){
		
		return "page/systemSetting/roleAdd";
	}
	/**
	 * 添加角色
	 * @return
	 */
	@RequestMapping("/role/addRole")
	@ResponseBody
	@RequiresPermissions("roleInfo:add")
	public String addRoles(SysRoles role){
		ReturnUtil res = new ReturnUtil();
		try {
			roleService.addRoles(role);
			res.setResult(true);
		} catch (Exception e) {
			res.setResult(false);
			res.setMsg("添加失败，请稍后重试！");
		}
		return JSON.toJSONString(res);
	}
	/**
	 * 修改
	 * @return
	 */
	@RequestMapping("/role/updateRole")
	@ResponseBody
	public String updateRoles(SysRoles role){
		ReturnUtil res = new ReturnUtil();
		try {
			roleService.updateRoles(role);
			res.setResult(true);
		} catch (Exception e) {
			res.setResult(false);
			res.setMsg("修改失败，请稍后重试！");
		}
		return JSON.toJSONString(res);
	}
	
	/**
	 * 删除
	 * @return
	 */
	@RequestMapping("/role/deleteRole")
	@ResponseBody
	public String deleteRoles(Long id){
		ReturnUtil res = new ReturnUtil();
		try {
			roleService.deleteRoles(id);
			res.setResult(true);
		} catch (Exception e) {
			res.setResult(false);
			res.setMsg("数据异常，请刷新后重试");
		}
		return JSON.toJSONString(res);
	}
	
}
